<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title><%= title %></title>
    <!-- <script src="/assets/lib/jquery/3.6.0/jquery.min.js"></script> -->
    <script src="/assets/lib/axios/0.26.1/axios.min.js"></script>
    <script src="/assets/javascripts/httpRequestUtils.js"></script>
    <script src="/assets/javascripts/localStorageUtils.js"></script>
    <script src="/assets/lib/sweetalert/2.1.2/sweetalert.min.js"></script>
    <script>
        // API地址
        const APIHOST = '<%= global.site.api.prefix %>';
        axios.defaults.baseURL = APIHOST;
    </script>
</head>

<body>
    <p id="displayText">
        正在跳转中，请稍候...
    </p>
    <script>
        // 带 token 的为绑定第三方账号，不带 token 的为第三方登录
        var token = localStorageUtils.getToken();
        getRequest("/third-party/callback/<%=platform%>" + location.search + (token ? ("&token=" + token) : ""))
            .then(function (response) {
                var axiosData = response.data;
                var status = axiosData.status;
                var data = axiosData.data;
                if (status === "success") {
                    console.log(data)
                    // 默认直接跳转 user 后台，如果是管理员则由 user 后台跳转
                    if (token) {
                        // 绑定第三方账号

                        // 绑定第三方账号成功
                        swal("绑定成功").then(() => {
                            location.href = "/dashboard/user/my-account";
                        });
                    } else {
                        // 第三方登录成功
                        localStorageUtils.userLogin({
                            token: data.token,
                            is_admin: data.group === "ADMIN",
                        });
                        if (localStorageUtils.getIsAdmin()) {
                            window.location.href = "/dashboard/admin/index";
                        } else {
                            window.location.href = "/dashboard/user/index";
                        }
                    }
                } else {
                    swal(`出错啦！${data.errMsg} (错误码: ${data.errCode})`).then(function () {
                        location.replace("/login");
                    });
                }
            }).catch(function (error) {
                console.log(error);
                document.getElementById("displayText").innerHTML = "系统错误，请稍后再试！";
                document.getElementById("displayText").style.color = "red";
            });
    </script>
</body>

</html>